package com.test.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.test.model.UserModel;

@WebFilter(urlPatterns = "/*", filterName = "rest0PubFilter")
public class LoginFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		HttpServletRequest request2 = (HttpServletRequest)request;
		HttpServletResponse response2 = (HttpServletResponse) response;
		UserModel userModel = (UserModel) request2.getSession().getAttribute("user");
		if(request2.getRequestURI().contains("login")||request2.getRequestURI().contains("insert")){
			chain.doFilter(request2, response2);
			return ;
		}
		if(userModel!=null){
			chain.doFilter(request2, response2);
			return ;
		}
		else {
			response2.sendRedirect("insert");
			return ;
		}
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	

}
